package mid;

import org.junit.Test;

public class a1442_形成两个异或相等数组的三元组数目 {

    public int countTriplets2(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            int sum = arr[i];
            for (int k = i + 1; k < arr.length; k++) {
                sum ^= arr[k];
                if (sum == 0) {
                    count += k - i;
                }
            }
        }
        return count;
    }

    public int countTriplets(int[] arr) {
        int count = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int k = i + 1; k < arr.length; k++) {
                if (m(arr, i, k)) count += m2(arr, i, k);
            }
        }
        return count;
    }

    private boolean m(int[] arr, int i, int k) {
        int s = 0;
        for (int j = i; j <= k; j++) {
            s ^= arr[j];
        }
        return s == 0;
    }

    private int m2(int[] arr, int i, int k) {
        int n1, n2, count = 0;
        int index = i + 1;
        while (index <= k) {
            n1 = n2 = 0;
            for (int ii = i; ii < index; ii++) {
                n1 ^= arr[ii];
            }
            for (int ii = index; ii <= k; ii++) {
                n2 ^= arr[ii];
            }
            if ((n1 ^ n2) == 0) {
                count++;
            }
            index++;
        }
        return count;
    }

    @Test
    public void test() {
        int[] arr = new int[]{2, 3, 1, 6, 7};
        System.out.println(countTriplets2(arr));
    }
}
